package com.yhb.service.impl;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import com.yhb.service.TestService;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Log4j
@Service
public class TestServiceImpl implements TestService {

    private final static Logger logger = Logger.getLogger(TestServiceImpl.class);

    @Autowired(required = false)
    private Producer producer;

    @Override
    public void produceMessage() {
        Message msg = new Message("pos", "TAG", "helloworld".getBytes());
        msg.setKey("testKey");
        try {
            SendResult result = producer.send(msg);
            assert result != null;
            logger.info("send msd success" + result.getMessageId());
        } catch (ONSClientException e) {
            logger.warn("send msg err", e);
        }
    }

    @Async
    @Override
    public void async() {
        log.info("from async");
    }
}
